package com.module.codeBind.dao;

import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;

import com.module.codeBind.vo.BatchCodeItem;
import com.module.codeBind.vo.BatchCodeItemSyn;

public class BatchCodeItemSynDao extends GenericHibernateDao<BatchCodeItemSyn,Long>{
	private static BatchCodeItemSynDao dao = new BatchCodeItemSynDao();
	private BatchCodeItemSynDao(){}
	public static BatchCodeItemSynDao getInstance(){
		return dao;
	}
	/**
	 * 添加保存操作记录（批次对应卷增量表）
	 * @param session
	 * @param itemList
	 * @param batchCodeId
	 * @return
	 */
	public boolean saveCodeItemSyn(Session session,List<BatchCodeItem> itemList,List<String> eldCodeItemId,String batchCodeId){
		boolean result  = true;
		if(eldCodeItemId.size()>0)
		result = deleteItemsyn(session, eldCodeItemId, batchCodeId);
		if(result){
			try {
				for(BatchCodeItem item:itemList){
					BatchCodeItemSyn batchCodeItemSyn = new BatchCodeItemSyn();
					PropertyUtils.copyProperties(batchCodeItemSyn,item);//对比数据，并构建目标对象
					batchCodeItemSyn.setSyscStatus(CUDEnum.C.toString());
					batchCodeItemSyn.setIssysc("1");
					session.save(batchCodeItemSyn);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return true;
	}
	/**
	 * 添加删除操作记录（批次对应卷增量表）
	 * @param session
	 * @param itemList
	 * @param batchCodeId
	 * @return
	 */
	public boolean deleteItemsyn(Session session,List<String> eldCodeItemId,String batchCodeId){
		boolean result = false;
		for(String item:eldCodeItemId){
			BatchCodeItemSyn batchCodeItemSyn = new BatchCodeItemSyn();
			batchCodeItemSyn.setId(item);
			batchCodeItemSyn.setBatchCodeId(batchCodeId);
			batchCodeItemSyn.setIssysc("1");
			batchCodeItemSyn.setSyscStatus(CUDEnum.D.toString());
			session.save(batchCodeItemSyn);
			result = true;
		}
		return result;
	}
}
